package 力扣._24_两两交换链表相邻结点;


class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

class Solution {
    public ListNode swapPairs(ListNode head) {
        head = new ListNode(0,head);     //补上头结点
        ListNode pre = head, p1 = head.next, p2;
        while (p1 != null && p1.next != null) {
            p2 = p1.next;
            p1.next = p2.next;
            p2.next = p1;
            pre.next = p2;
            p1 = p1.next;
            pre = pre.next.next;
        }
        return head.next;
    }
}
